<?php


namespace app\api\controller;


use think\Request;

class Activity extends Controller
{

    /**
     * 获取活动列表
     * @return \think\response\Json
     */
    public function list(){
        //返回值
        $rdata = array('res_code'=>0,'res_meg'=>"访问成功",'list'=>array());
        $rdata['list'] = $this->getListData();
        return json($rdata);
    }

    public function activityinfo(){
        //返回值
        $rdata = array('res_code'=>0,'res_meg'=>"访问成功",'list'=>array());
        $activityId = Request::param("activity_id");
        if(empty($activityId)){
            $rdata['res_code'] = 9999;
            $rdata['res_meg'] = "活动id不能为空";
            return json($rdata);
        }
        $rdata['list'] = $this->getOneData($activityId);
        return json($rdata);
    }

    /**
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function luckydraw(){
        $rdata = array('res_code'=>0,'res_meg'=>"恭喜中奖");
        $user = $this->getUser();
        $activityId = Request::param("activity_id");

        $wc = $this->judegluckydraw($user['user_id'],$activityId);
        if($wc >0){
            $rdata['res_code'] = 9999;
            $rdata['res_meg'] = "您已经中过奖";
            return $rdata;
        }



    }


    /**
     * 获取活动那个列表
     * @return bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getListData(){
        $wdata = array();
        $wdata['is_use'] = 1;
        return \app\api\model\Activity::getAlist($wdata);
    }

    /**
     * 或一条数据
     * @param $activityId
     * @return array|bool|false|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneData($activityId){
        $wdata = array();
        $wdata['activity_id'] =$activityId;
        return \app\api\model\Activity::getAoneData($wdata);
    }

    /**
     * @param $userId
     * @param $activityId
     * @return int|string
     * @throws \think\Exception
     */
    public function judegluckydraw($userId,$activityId){
        $wdata = array();
        $wdata['user_id'] =$userId;
        $wdata['activity_id'] =$activityId;
        return \app\api\model\Activity::getAwin($wdata);
    }
}